package com.neusoft.neuiotms.dcvm.repository.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcvm.model.Model;
import com.neusoft.neuiotms.dcvm.model.QModel;
import com.neusoft.neuiotms.dcvm.repository.CustomizedModelRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedModelRepositoryImpl extends AbstractRepository implements CustomizedModelRepository {

    @Override
    public Page<Model> search(Pageable pageable, Searchable searchable) {
        final QModel qModel = QModel.model;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<Model> query = this.query().selectFrom(qModel);
        query.where(where);
        return super.search(query, pageable);
    }

    private BooleanBuilder buildWhere(Searchable searchable) {
        final QModel qModel = QModel.model;
        final BooleanBuilder where = new BooleanBuilder();
        where.or(super.containsStr(qModel.modelName, searchable, "kw"));
        where.or(super.containsStr(qModel.modelCode, searchable, "kw"));
        return where;
    }

    @Override
    protected Class<?> getModelClass() {
        return Model.class;
    }

}
